/**
 * 
 */
package slhs.tempo.scriptGenerator;

import java.awt.event.ActionEvent;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 * ButtonsPanelHandler.java
 * 
 * @author Swapnil Devikar
 * 
 */
public class SegmentConfigurationButtonsPanelHandler extends JPanel implements PanelHandler {

    private JPanel buttonsPanel;
    private JButton doneButton;
    private JButton backButton;

    private String fileToBeImported;

    private SegmentConfigurationPanelHandler segmentConfigurationPanelHandler;

    public SegmentConfigurationButtonsPanelHandler(SegmentConfigurationPanelHandler segmentConfigurationPanelHandler) {
	this.segmentConfigurationPanelHandler = segmentConfigurationPanelHandler;
	initComponents();
	addComponents();
    }

    /**
     * Set the excel file path
     * */
    public void setExcelFilePath(String fileToBeImported) {
	this.fileToBeImported = fileToBeImported;
    }

    /**
     * 
     */
    private void addComponents() {
	buttonsPanel.add(backButton);
	buttonsPanel.add(doneButton);
    }

    /**
     * 
     */
    private void initComponents() {
	buttonsPanel = new JPanel();
	doneButton = new JButton("Done");
	backButton = new JButton("Back");

	// action listeners
	doneButton.addActionListener(this);
	backButton.addActionListener(this);

    }

    /*
     * (non-Javadoc)
     * 
     * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
     */
    @Override
    public void actionPerformed(ActionEvent event) {
	if (event.getSource().equals(doneButton)) {
	    String excelFilePath = segmentConfigurationPanelHandler.getBrowseFileTextFieldText();
	    boolean importingFromExcel = segmentConfigurationPanelHandler.isImportingFromExcel();
	    if (importingFromExcel && (excelFilePath == null)) {
		JOptionPane.showMessageDialog(null, "Please choose XLS file to import from");
		segmentConfigurationPanelHandler.highlightBrowseFileTextField();
	    } else {
		ScriptDisplayAndEditor scriptDisplayAndEditor = new ScriptDisplayAndEditor(
			segmentConfigurationPanelHandler.getData(fileToBeImported));
		scriptDisplayAndEditor.showScript(segmentConfigurationPanelHandler);
	    }

	}
    }

    /*
     * (non-Javadoc)
     * 
     * @see slhs.tempo.scriptGenerator.PanelHandler#getPanel()
     */
    @Override
    public JPanel getPanel() {
	// TODO Auto-generated method stub
	return buttonsPanel;
    }

    protected JButton getDoneButton() {
	return this.doneButton;
    }

    protected JButton getBackButton() {
	return this.backButton;
    }

}
